package com.seed.bpmn.engine.common.util;


/**
 * 分页对象
 *
 * @author cscz89@126.com
 */
public class Page {

    /** 默认分页大小：15*/
    public static final int DEFAULT_PAGE_SIZE = 15;

    /** 默认页码：1*/
    public static final int DEFAULT_PAGE_NUMBER = 1;

    // 总记录数
    private int totalObjects = 0;

    // 当前页数
    private int pageIndex = 1;

    // 每页记录数
    private int pageSize = DEFAULT_PAGE_SIZE;

    // 查询记录起始位置，如果为空则按pogeNumber来查询。
    private int start = -1;

    // 按哪个字段排序 new String[]{"ID","DESC"}
    private String[][] sortProperty;

    /**
     * 通过计算返回查询记录的起始位置。
     * @return
     */
    public int getFirstResult() {
        if(this.start>=0){
            if(this.start < this.totalObjects){
                return this.start;
            }else{
                return 0;
            }
        }
        // 计算第一条记录
        int totalPages2 = this.getTotalPages();
        if (totalPages2 < pageIndex && pageIndex > 1){
            pageIndex = totalPages2;
        }
        return (pageIndex - 1) * pageSize;
    }

    /**
     * 获取总页数
     * @return
     */
    public int getTotalPages() {
        if (totalObjects % pageSize == 0)
            return totalObjects / pageSize;
        else
            return (totalObjects / pageSize) + 1;
    }

    /**
     * @return 总记录数
     */
    public long getTotalObjects() {
        return totalObjects;
    }

    /**
     * 设置总记录数
     * @param totalObjects
     */
    public void setTotalObjects(int totalObjects) {
        this.totalObjects = totalObjects;
    }

    public int getPageIndex() {
        if (getTotalObjects() == 0) {
            return 1;
        }
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex < 1 ? 1 : pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String[][] getSortProperty() {
        return sortProperty;
    }

    public void setSortProperty(String[][] sortProperty) {
        this.sortProperty = sortProperty;
    }

    public void setStart(int start) {
        this.start = start;
    }
}
